//
//  MLWReadViewController.m
//  KITCHEN_STORIES
//
//  Created by dllo on 16/6/14.
//  Copyright © 2016年 MLW. All rights reserved.
//

#import "MLWReadViewController.h"
#import "MLWxxreadTableViewCell.h"
#import "MLWxxReadModel.h"
#import "UIImageView+WebCache.h"
@interface MLWReadViewController ()<UITableViewDelegate, UITableViewDataSource>
/** tableView */
@property (nonatomic, retain) UITableView *readTableView;

/** 数据源数组 */
@property (nonatomic, retain) NSMutableArray *dataSourceArray;
@end

@implementation MLWReadViewController

- (void)dealloc
{
    [self.readTableView release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //数据请求:
    [self getDataSource];
    self.dataSourceArray = [NSMutableArray array];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"精品阅读";
    
    self.readTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    //设代理
    self.readTableView.delegate = self;
    self.readTableView.dataSource = self;
    
    [self.view addSubview:self.readTableView];
    [self.readTableView release];
    
    //注册:
    [self.readTableView registerClass:[MLWxxreadTableViewCell class] forCellReuseIdentifier:@"mlvxxreadTableViewCell"];
}

//数据请求:
-(void)getDataSource
{
    [MLWNetTool GetNetWithUrl:@"http://api.haodou.com/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&appsign=2765061d1d8879ffb299900f98188bcf&channel=appstore&deviceid=0f607264fc6318a92b9e13c65db7cd3c%7C16CB50EE-9F72-48B3-93E6-BB8F3236BC3E%7C0554EFA2-6CA4-48D7-9160-4A337D7AC35D&format=json&loguid=&method=Read.getList&nonce=1465903040&sessionid=1465898029&signmethod=md5&timestamp=1465903040&uuid=35f8a59b94507888a393c7237587fecf&v=2&vc=49&vn=v6.1.1" body:nil header:nil response:MLWJSON success:^(id result) {
        
        NSDictionary *dic = (NSDictionary *)result;
        
        NSDictionary *dicResult = [dic objectForKey:@"result"];
        NSMutableArray *listArray = [dicResult objectForKey:@"list"];
        for (NSDictionary *dict in listArray) {
            
            MLWxxReadModel *model = [[MLWxxReadModel alloc] initWithDic:dict];
            
            [self.dataSourceArray addObject:model];
            
           
            [model release];
        }
        
        [self.readTableView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"请求失败");
    }];
}


#pragma mark --- 必须实现的代理方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MLWxxreadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mlvxxreadTableViewCell"];
   MLWxxReadModel *model = [self.dataSourceArray objectAtIndex:indexPath.row];

    [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:model.Img]placeholderImage:[UIImage imageNamed:@"placeholder"]];
    
    cell.titleLabel.text = model.Title;
    cell.contentLabel.text = model.Intro;
   
       return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
